package cn.rengy.web.framework.sys.user.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import cn.rengy.web.framework.principal.Identity;
import cn.rengy.web.framework.property.service.PropertyService;
import cn.rengy.web.framework.sql.DbService;
import cn.rengy.web.framework.sys.user.AuthTable;
import cn.rengy.web.framework.sys.user.entity.SysUser;

@Service
public class UserService {
	@Autowired
	private DbService dbService;
	@Autowired
	private PropertyService propertyService;
	private static Logger logger = LoggerFactory.getLogger(UserService.class);
	private PasswordService passwordService;
	@Autowired
	private CacheManager cacheManager;
	@Autowired
	public void setPasswordService(PasswordService passwordService) {
		this.passwordService = passwordService;
	}
	
	public SysUser getSysUser(String user_id) {
		return this.dbService.queryForBean("select_get_sys_user", SysUser.class, user_id);
	}
	/**
	 * 绑定登录账号
	 * @param paramMap
	 */
	public void bindLogin(Map<String,Object> paramMap) {
		String auth_id=MapUtils.getString(paramMap, "auth_id");
		String user_id=MapUtils.getString(paramMap, "user_id");
		this.dbService.update("insert_sys_user_auth_rel",auth_id, user_id,AuthTable.sys_login.name());
	}
	/**
	 * 重置密码
	 * @throws Exception 
	 */
	public void resetpwd(Map<String,Object> paramMap) {
		String defaultPassword=propertyService.getProperty("DEFAULT_PASSWORD");
		defaultPassword=passwordService.encryptPassword(defaultPassword);
		List<Object> rowids=(List)paramMap.get("ids");
		if(rowids!=null){
			for(Object id:rowids){
				this.dbService.update("update_user_password", defaultPassword,id);
			}
			
		}
		
	}
	/**
	 * 修改密码
	 * @throws Exception 
	 */
	public void changePassword(Map<String,Object> paramMap) {
		String password=(String)paramMap.get("password");
		Subject subject = SecurityUtils.getSubject();
		Identity identity=(Identity)subject.getPrincipal();
		String userLoginId=identity.getAuthid().toString();
		password=passwordService.encryptPassword(password);
		this.dbService.update("update_user_password", password,userLoginId);
	}
	/**
	 * 根据用户名查询认证信息
	 * @param username
	 * @return
	 */
	public Map<String,Object> getUserByUserName(String username){
		Map<String,Object> user=dbService.queryForMap("select_get_user_login",username);
		return user;
	}
	
	public void updateUserInfo(Map<String,Object> user) {
		this.dbService.updateMapParameter("update_user_unionid", user);
		evictUserInfoCache((Long)user.get("id"));
	}
	public void evictUserInfoCache(Long userid) {
		Cache cache=cacheManager.getCache("userInfoCache");
		cache.evict(userid.toString());
	}
	@Autowired(required = false)
	private AdditionalUserInfoService additionalUserInfoService;
	
	
	
	/*
	 * public Map<String,Object> getUserInfo(String userid){ Cache
	 * cache=cacheManager.getCache("userInfoCache");
	 * 
	 * Map<String,Object> map=null; if(userid!=null) { String key=userid.toString();
	 * ValueWrapper valueWrapper=cache.get(key); if(valueWrapper==null){
	 * map=this.dbService.queryForMap("select_get_sys_user", userid);
	 * Map<String,Object>
	 * additionalUserInfo=additionalUserInfoService.getUserInfo(userid);
	 * if(map!=null){ if(additionalUserInfo!=null) { map.putAll(additionalUserInfo);
	 * } cache.put(key, map); }
	 * 
	 * }else{ map= (Map<String,Object>)valueWrapper.get(); } } return map; }
	 */
}
